package Conexiones;


import java.sql.*;

public class MySQLClass {

      private Connection conn;
      private ResultSet rs;
      private Statement instruc;

      private static String ip = "localhost";
      private static String db_name = "Videos";
      private static String user = "root";
      private static String passwd = "Lacrimosa14";



    public MySQLClass()
    {

                  try{

           Class.forName("org.gjt.mm.mysql.Driver");

           conn = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/"+db_name, user, passwd);

           //System.out.println("Driver org.gjt.mm.mysql.Driver in use");
           //System.out.println("");

           instruc=(Statement) conn.createStatement();
           }

           catch(Exception e)
           {
                 System.out.println("Error en o al cargar ");
                 System.out.println(e.getMessage());
                 System.exit(0);
           }


    }


    public ResultSet exec( String StrSql )
    {
       try{
           /*System.out.println("--------------------------------------------");
           System.out.print("( " + StrSql + " )");
           System.out.print(" Only First and Last Rows ");
           System.out.println("  ");
           */
           rs = (ResultSet) instruc.executeQuery(StrSql);

           ResultSetMetaData rsMetaData = rs.getMetaData();
           int numberOfColumns = rsMetaData.getColumnCount();


            //System.out.println("--------------------------------------------");

            for (int i = 1; i < numberOfColumns + 1; i++)
            {
              String columnName = rsMetaData.getColumnName(i);
              //System.out.print("- " + columnName + " ");
            }

            /*System.out.print("-");
            System.out.println("");
            System.out.println("--------------------------------------------");*/




            while(rs.next())
                  {
                        for (int i = 1; i < numberOfColumns + 1; i++)
                        {
                        String columnName = rsMetaData.getColumnName(i);
                        //System.out.print( rs.getString(columnName) + " ");
                        }

                   rs.last();
                   //System.out.println("");

                        for (int i = 1; i < numberOfColumns + 1; i++)
                        {
                        String columnName = rsMetaData.getColumnName(i);
                        //System.out.print( rs.getString(columnName) + " ");
                        }
                  }

            //System.out.println("");
            //System.out.println("--------------------------------------------");

          }
       catch(Exception e)
         {
           System.out.println("Error en la base de datos");
           System.out.println(e.getMessage());
         }

       return rs;
      }



}
